package com.maxlen.goods.controller;

import com.maxlen.common.utils.CopyUtils;
import com.maxlen.common.utils.PageUtils;
import com.maxlen.common.utils.R;
import com.maxlen.goods.entity.CategoryBrandRelationEntity;
import com.maxlen.goods.entity.dto.CategoryBrandRelationWithBrandDTO;
import com.maxlen.goods.entity.vo.CategoryBrandRelationVO;
import com.maxlen.goods.entity.dto.CategoryBrandRelationDTO;
import com.maxlen.goods.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 品牌分类关联
 *
 * @author MaxLen
 * @email MaxLen@gmail.com
 * @date 2022-10-27 10:12:58
 */
@RestController
@RequestMapping("goods/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 根据品牌 id 查询相关分类列表
     */
    @GetMapping("/catelog/list")
    // @RequiresPermissions("goods:categorybrandrelation:list")
    public R catelogList(@RequestParam Long brandId){
        List<CategoryBrandRelationEntity> entityList = categoryBrandRelationService.findByBrandId(brandId);
        List<CategoryBrandRelationDTO> relationVoList = CopyUtils.copyList(entityList, CategoryBrandRelationDTO.class);
        return R.ok().put("data", relationVoList);
    }

    /**
     * 根据分类 id 查询相关品牌列表
     */
    @GetMapping("/brands/list")
    // @RequiresPermissions("goods:categorybrandrelation:list")
    public R brandList(@RequestParam(required = false) Long catId){
        List<CategoryBrandRelationEntity> entityList = categoryBrandRelationService.findByCategoryId(catId);
        List<CategoryBrandRelationWithBrandDTO> relation = CopyUtils.copyList(entityList, CategoryBrandRelationWithBrandDTO.class);
        return R.ok().put("data", relation);
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    // @RequiresPermissions("goods:categorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    // @RequiresPermissions("goods:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);
        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    // @RequiresPermissions("goods:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationVO categoryBrandRelationVO){
		categoryBrandRelationService.saveDetail(categoryBrandRelationVO);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    // @RequiresPermissions("goods:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    // @RequiresPermissions("goods:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
